package com.movisol.messenger.receivers;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.movisol.c2dm.R;
import com.movisol.c2dm.receivers.C2DMBaseReceiver;
import com.movisol.messenger.controllers.MessengerController;
import com.movisol.messenger.views.Chat;
import com.movisol.messenger.views.MainPage;
import com.movisol.messenger.views.WaitingListPage;
import com.movisol.messengercommunicator.helper.MessengerCommunicatorHelper;

public class C2DMReceiver extends C2DMBaseReceiver {
	private static MessengerController mc = MessengerController.getInstance();
	private static final int MESSAGE = 1;
	private static final int INVITATION = 2;
	private String msg = null;
	private String sender = null;
	private String ticketText = null;
	private String ci = null;
	private String sui = null;
	private int pushType = 0;

	
    public C2DMReceiver() {  
        // Email address currently not used by the C2DM Messaging framework  
        super("android@movisol.com");  
    }  
  
    @Override  
    public void onRegistered(Context context, String registrationId) {  
        Log.d("C2DM", "Registration ID arrived: Fantastic!!!");  
        Log.d("C2DM", registrationId);
		fireRegistrationIdReceived(registrationId);

    }  
  
    @Override  
    protected void onMessage(Context context, Intent intent) {  
        Log.d("C2DM", "Message: Fantastic!!!");
        
        String type = null;
        if(intent.getExtras() != null)
        {
        	type = intent.getStringExtra("type");
        }
        
        if(type != null)
        {
    		
        	if(type.equals("c-invRec"))
        	{
        		sui = intent.getStringExtra("sui");
        		sender = intent.getStringExtra("sn");
        		ticketText = "New Contact!";
        		msg = "Wants to add you!";
        		pushType = INVITATION;
        	}
        	
        	if(type.equals("c-msgRec"))
        	{
        		msg = intent.getStringExtra("message");
        		sender = intent.getStringExtra("sender");
        		ci = intent.getStringExtra("ci");
        		ticketText = "Message received!";
        		pushType = MESSAGE;
        	}

        	switch(pushType)
        	{
        	case MESSAGE:
				if (mc.getCurrentPage() == MessengerController.CHAT_PAGE && ci != null
						&& mc.getCurrentConversation() != null
						&& ci.equals(mc.getCurrentConversation().getConversationId()))
					firePushNotificationReceived();
				else
					createNewMessageNotification(context, type);
        		break;
        	case INVITATION:
				if (mc.getCurrentPage() == MessengerController.WAITING_PAGE) {
					mc.c2dmc.setSui(sui);
					firePushNotificationReceived();
				} else
					createInvitationNotification(context, type);

        		break;
        	}
        }
    }  
  
    @Override  
    public void onError(Context context, String errorId) {  
        Log.d("C2DM", "Error occured!!!");  
    }  
  
	public void createNewMessageNotification(Context context, String type) {
		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.ic_conversations_btn_pressed,
				ticketText, System.currentTimeMillis());
		// Hide the notification after its selected
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		Intent intent = new Intent();
		intent.putExtra("ci", ci);
		intent.putExtra("type", type);
		
		switch(mc.getCurrentPage())
		{
			case 0: //APP NOT RUNNING
				intent.setClass(context, MainPage.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
				
				break;
			default: //ANY OTHER PAGE
			intent.setClass(context, Chat.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.putExtra("pushReceived", true);
				mc.setCurrentConversation(MessengerCommunicatorHelper.findConversationByConversationId(ci, mc.cc.getConversationsList().
						getConversations()));
				break;
			
		}
		
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		notification.setLatestEventInfo(context, sender, msg, pendingIntent);		
		notificationManager.notify(0, notification);

	}
	
	public void createInvitationNotification(Context context, String type) {
		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.ic_conversations_btn_pressed,
				ticketText, System.currentTimeMillis());
		// Hide the notification after its selected
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		Intent intent = new Intent();
		intent.putExtra("sui", sui);
		intent.putExtra("type", type);
		switch(mc.getCurrentPage())
		{
			case 0: //APP NOT RUNNING
				intent.setClass(context, MainPage.class);
				intent.putExtra("loadFriendList", true);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
				break;
			default: //ANY OTHER PAGE
				intent.setClass(context, WaitingListPage.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//				mc.setCurrentConversation(MessengerCommunicatorHelper.findConversationByConversationId(ci, mc.cc.getConversationsList().getConversations()));
				break;
			
		}
	
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		notification.setLatestEventInfo(context, sender, msg, pendingIntent);
		notificationManager.notify(0, notification);

	}
}  